package com.uex.dev.manage.common.generator;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;
import java.util.function.Consumer;

/**
 * mybatis-plus代码生成器
 * <p>
 * mybatis-plus官方文档地址：https://baomidou.com/pages/779a6e/#%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8
 * </p>
 *
 * @author zixia_liang
 * @date 2023-10-30 14:55
 */
public class MybatisPlusGenerator {

    private static final String URL = "jdbc:mysql://192.168.2.53:3306/uex_device_manager?useUnicode=true&characterEncoding=UTF-8&useSSL=false&useTimezone=true&serverTimezone=GMT%2B8";
    private static final String USER_NAME = "admin";
    private static final String PASSWORD = "mysql";

    //作者
    private static final String AUTHOR = "wangchao";
    //输出目录, 可以修改为项目对于的目录，直接生成到项目的目录中
    private static final String OUTPUT_DIR = "D://MybatisPlusGenerator";
    //包名
    private static final String PARENT_PACKAGE = "com.uex.dev.manage";
    //模块名
    private static final String MODULE_NAME = "admin";

    /**
     * 需要生成代码的表，如果生成所有表的代码，传空数组
     */
    private static final String[] TABLES = new String[]{"t_device_model","t_model_robot","t_model_uhub"};

    /**
     * 过滤表名的前缀，如t_前缀的表 t_device 生成的类不会有t开头
     */
    private static final String[] TABLE_PREFIX = new String[]{"t_"};

    /**
     * 数据源配置
     */
    public static void main(String[] args) {
        FastAutoGenerator.create(URL, USER_NAME, PASSWORD)
                //全局配置
                .globalConfig(GLOBAL_CONFIG_CONSUMER)
                //包配置
                .packageConfig(PACKAGE_CONFIG_CONSUMER)
                //策略配置
                .strategyConfig(STRATEGY_CONFIG_CONSUMER)
                .templateEngine(new FreemarkerTemplateEngine())
                .templateConfig(TEMPLATE_CONFIG_CONSUMER)
                .execute();
    }

    /**
     * 全局配置
     */
    private static final Consumer<GlobalConfig.Builder> GLOBAL_CONFIG_CONSUMER = builder -> {
        builder.author(AUTHOR) // 设置作者
//                .enableSwagger() // 开启 swagger 模式
                .outputDir(OUTPUT_DIR); // 指定输出目录
    };

    /**
     * 包配置
     */
    private static final Consumer<PackageConfig.Builder> PACKAGE_CONFIG_CONSUMER = builder -> {
        builder.parent(PARENT_PACKAGE) // 设置父包名
                .moduleName(MODULE_NAME) // 设置父包模块名
                .entity("po")
                .service("service")
                .serviceImpl("service.impl")
                .mapper("dao")
                .xml("mapper.xml")
                .controller("controller")
                .pathInfo(Collections.singletonMap(OutputFile.xml, OUTPUT_DIR + PARENT_PACKAGE.replaceAll("\\.", "/") + "/mapper")); // 设置mapperXml生成路径
    };

    /**
     * 模版配置
     */
    private static final Consumer<TemplateConfig.Builder> TEMPLATE_CONFIG_CONSUMER = builder -> {
        builder.disable(TemplateType.ENTITY)
                .entity("/templates/entity.java")
                .service("/templates/service.java")
                .serviceImpl("/templates/serviceImpl.java")
                .mapper("/templates/mapper.java")
                .xml("/templates/mapper.xml")
                .controller("/templates/controller.java");

    };

    /**
     * 策略配置
     */
    private static final Consumer<StrategyConfig.Builder> STRATEGY_CONFIG_CONSUMER = builder -> {
        builder.addInclude(TABLES) // 设置需要生成的表名
                .addTablePrefix(TABLE_PREFIX) // 设置过滤表前缀

                //Entity 策略配置
                .entityBuilder()
                .enableChainModel()
                .enableLombok()
                .enableRemoveIsPrefix()
                .enableActiveRecord()
                .logicDeleteColumnName("deleted") //逻辑删除字段名
                .idType(IdType.AUTO) //主键生成策略
                .formatFileName("%sPO")
                .enableFileOverride()


                //Service 策略配置
                .serviceBuilder()
                .enableFileOverride()

                //Mapper 策略配置
                .mapperBuilder()
                .superClass(BaseMapper.class)
                .mapperAnnotation(org.apache.ibatis.annotations.Mapper.class)
                .enableBaseResultMap()
                .enableBaseColumnList()
                .enableFileOverride();

    };
}
